package com.fortune.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fortune.dao.DOrderDao;
import com.fortune.dto.in.dOrder.DOrderAddDto;
import com.fortune.dto.in.dOrder.DOrderDetailAddDto;
import com.fortune.dto.in.dOrder.DOrderEditDto;
import com.fortune.dto.in.dOrder.DOrderListDto;
import com.fortune.entity.DUserInfo;
import com.fortune.entity.MProductInfo;
import com.fortune.service.DOrderService;
import com.fortune.vo.dOrder.DOrderDetail;
import com.fortune.vo.dOrder.DOrderInfo;
@Service
public class DOrderServiceImpl implements DOrderService {
	@Autowired
	private DOrderDao dOrderDao;
	
	@Override
	public List<DOrderInfo> list(DUserInfo dUserInfo, DOrderListDto  dOrderListDto) {
		List<DOrderInfo> list = dOrderDao.list(dUserInfo,dOrderListDto);
		return list;
	}
	
	@Override
	public int count(DUserInfo dUserInfo, DOrderListDto  dOrderListDto) {
		int count = dOrderDao.count(dUserInfo,dOrderListDto);
		return count;
	}
	
	@Override
	public DOrderDetail detail(DUserInfo dUserInfo, String orderId) {
		DOrderDetail detail = dOrderDao.detail(dUserInfo, orderId);
		return detail;
	}

	@Override
	@Transactional
	public int add(DOrderAddDto addDto,DUserInfo dUserInfo) {
		String id = UUID.randomUUID().toString().replace("-","");
		List<DOrderDetailAddDto> list = addDto.getDetails();
		BigDecimal pTotalPrice = null;
		BigDecimal dTotalPrice = null;
		for(DOrderDetailAddDto dOrderDetailAddDto: list){
			MProductInfo mProductInfo = dOrderDao.mProductInfo(dUserInfo,dOrderDetailAddDto);
			if(mProductInfo==null)
				return 2;
			BigDecimal price = mProductInfo.getWholesalePrice();
			BigDecimal boxPrice = mProductInfo.getBoxPrice();
			int num = dOrderDetailAddDto.getOrderNum();
			BigDecimal totalPrice = price.multiply(new BigDecimal(num));
			BigDecimal boxTotalPrice = boxPrice.multiply(new BigDecimal(num));
			if(pTotalPrice==null){
				pTotalPrice = totalPrice;
			}else {
				pTotalPrice = pTotalPrice.add(totalPrice);
			}
			if(dTotalPrice==null){
				dTotalPrice = boxTotalPrice;
			}else {
				dTotalPrice = dTotalPrice.add(boxTotalPrice);
			}
			dOrderDao.addDetail(dUserInfo,dOrderDetailAddDto, id, totalPrice, boxTotalPrice, price);
		}
		int add = dOrderDao.add(dUserInfo,addDto,id,pTotalPrice,dTotalPrice);
		return add;
	}

	@Override
	public int edit(DOrderEditDto editDto,DUserInfo dUserInfo) {
		int delete = dOrderDao.deleteDetal(editDto, dUserInfo);
		String id = editDto.getOrderId();
		List<DOrderDetailAddDto> list = editDto.getDetails();
		BigDecimal pTotalPrice = null;
		BigDecimal dTotalPrice = null;
		for(DOrderDetailAddDto detailAddDto: list){
			MProductInfo mProductInfo = dOrderDao.mProductInfo(dUserInfo,detailAddDto);
			if(mProductInfo==null)
				return 2;
			BigDecimal price = mProductInfo.getWholesalePrice();
			BigDecimal boxPrice = mProductInfo.getBoxPrice();
			int num = detailAddDto.getOrderNum();
			BigDecimal totalPrice = price.multiply(new BigDecimal(num));
			BigDecimal boxTotalPrice = boxPrice.multiply(new BigDecimal(num));
			if(pTotalPrice==null){
				pTotalPrice = totalPrice;
			}else {
				pTotalPrice = pTotalPrice.add(totalPrice);
			}
			if(dTotalPrice==null){
				dTotalPrice = boxTotalPrice;
			}else {
				dTotalPrice = dTotalPrice.add(boxTotalPrice);
			}
			dOrderDao.addDetail(dUserInfo, detailAddDto, id, totalPrice, boxTotalPrice, boxTotalPrice);
		}
		int result = dOrderDao.edit(editDto, dUserInfo, pTotalPrice, dTotalPrice);
		return result;
	}

	@Override
	public int delete(DUserInfo dUserInfo, String orderId) {
		int result = dOrderDao.delete(dUserInfo, orderId);
		return result;
	}
}
